﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LineWatch_WinForm;
using System.Configuration;
using System.IO;
using LineWatch.Common;
using DBAccessLib;
using System.Threading;

namespace Linewatch_SetupForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ConfigInfo.InitAllPositions();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Setup setup = new Setup();
            setup.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SerialPortOperator sp = new SerialPortOperator("COM1", 9600);//ConfigurationManager.AppSettings["ComPort"], 9600);
            sp.WriteLine("@00WD50000000");// + ConfigurationManager.AppSettings["PlanNumDM"].PadLeft(4, '0') + "0000");
            Thread.Sleep(100);
            sp.WriteLine("@00WD11400000");// + ConfigurationManager.AppSettings["ProductNumDM"].PadLeft(4, '0') + "0000");
            Thread.Sleep(100);
            sp.WriteLine("@00WD10860000");// + ConfigurationManager.AppSettings["StopTimeDM"].PadLeft(4, '0') + "0000");
            Thread.Sleep(100);
            sp.ClosePort();
            MessageBox.Show("复位成功");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.csv)|*.csv";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    if (myStream == null)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }

            LinePositionModel model = new LinePositionModel();
            model.LinePositionTable = ConfigInfo.DtAllPositions.Clone();

            //create object of CSVReader and pass the stream

            Encoding encoding = Encoding.GetEncoding("GB2312");//ConfigurationManager.AppSettings["CSVEncoding"]);
            CSVReader reader = new CSVReader(myStream, encoding);

            string[] data = new string[100];
            int lineIndex = 0;

            while ((data = reader.GetCSVLine()) != null)
            {
                lineIndex++;
                if (lineIndex == 1)
                {
                    continue;
                }

                if (data.Length >= 5)
                {
                    DataRow newRow = model.LinePositionTable.NewRow();

                    newRow["Line_Code"] = data[0].Trim();
                    newRow["SPositionID"] = data[1].Trim();
                    newRow["FPositionID"] = data[2].Trim();
                    newRow["Position"] = data[3].Trim();
                    newRow["SetDate"] = string.Empty;
                    newRow["User"] = string.Empty;

                    if (!string.IsNullOrWhiteSpace(data[4].Trim()))
                    {
                        newRow["Content1"] = data[4].Trim();
                        newRow["Content2"] = "1";
                        newRow["Content3"] = "0";
                        newRow["Content4"] = "0";
                    }
                    else if ((data.Length >= 6) && !string.IsNullOrWhiteSpace(data[5].Trim()))
                    {
                        newRow["Content1"] = data[5].Trim();
                        newRow["Content2"] = "0";
                        newRow["Content3"] = "1";
                        newRow["Content4"] = "0";
                    }
                    else if ((data.Length >= 7) && !string.IsNullOrWhiteSpace(data[6].Trim()))
                    {
                        newRow["Content1"] = data[6].Trim();
                        newRow["Content2"] = "0";
                        newRow["Content3"] = "0";
                        newRow["Content4"] = "1";
                    }

                    newRow["Content5"] = string.Empty;
                    newRow["Content6"] = string.Empty;
                    model.LinePositionTable.Rows.Add(newRow);
                }
            }

            bool result = Position.SaveAllLinePositions(model);

            if (result)
            {
                MessageBox.Show("保存成功!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("保存失败!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (myStream != null)
            {
                myStream.Close();
            }
        }
    }
}
